#!/usr/bin/env python
# _*_ coding: utf-8 _*_
"""
author : DELL
project : ApiTestPlatform
date : 2021/11/22
"""

import yaml


class YamlHandle:
    def __init__(self, filepath):
        # :param filepath ：yaml文件路径
        self.filepath = filepath

    def read_yaml(self):
        with open(self.filepath, encoding='utf-8') as f:
            # yaml 5.1版后弃用了yaml.load(file)这个用法，因为觉得很不安全，5.1版后就修改了需要指定Loader，通过默认加载​​器（FullLoader）禁止执行任意函数
            data = yaml.load(f.read(), Loader=yaml.FullLoader)
        return data

    def writ_yaml(self, ):
        """
        :param key: 字典的key
        :param value: 写入的值
        :return:
        """
        with open(self.filepath,'r',encoding='utf-8') as f:
            doc = yaml.safe_load(f)

        for item in doc['users']:
            print(item)
            # item['o'] = '2'

        with open(self.filepath,'w',encoding='utf-8') as f:
            yaml.safe_dump(doc['users'],f,default_flow_style=False,allow_unicode=True)



if __name__ == '__main__':
    yamlfilepath = r'E:\codes\python\ApiTestPlatform\setting.yaml'
    yh = YamlHandle(yamlfilepath)
    # print(yh.read_yaml())
    # print(yh['users'])
    yh.writ_yaml()